package com.blogsystem.controller;

import com.blogsystem.entity.User;
import com.blogsystem.service.UserService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private UserService userService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private AuthenticationManager authenticationManager;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public String register(@RequestParam String username,
                           @RequestParam String password,
                           @RequestParam String confirmPassword,
                           @RequestParam(required = false) String email,
                           RedirectAttributes redirectAttributes) {
        if (userService.getOne(new QueryWrapper<User>().eq("username", username)) != null) {
            redirectAttributes.addAttribute("error", true);
            return "redirect:/register";
        }

        if (!password.equals(confirmPassword)) {
            redirectAttributes.addAttribute("error", true);
            return "redirect:/register";
        }

        String encodedPwd = passwordEncoder.encode(password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(encodedPwd);
        user.setEmail(email);
        user.setRole(0);
        // 设置默认头像URL
        user.setAvatar("/image/avatar0.jpg");
        userService.save(user);
        System.out.println("用户注册成功：" + user);
        redirectAttributes.addAttribute("success", true);
        return "redirect:/login";
    }

    @GetMapping("/logout")
    public String getLogout() {
        // 清除当前用户的认证信息，实现登出功能
        SecurityContextHolder.clearContext();
        // 登出后重定向到首页
        return "redirect:/index";
    }

    @GetMapping("/login")
    public String showLoginPage() {
        return "login"; // 返回login.html模板
    }

    @GetMapping("/register")
    public String showRegisterPage() {
        return "register"; // 返回register.html模板
    }
    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/me")
    public Object getCurrentUser(@AuthenticationPrincipal UserDetails userDetails) {
        if (userDetails == null) return null;
        // 你可以返回更多信息
        return userDetails.getUsername();
    }

}